﻿

Partial Class Login
    Inherits System.Web.UI.Page
    Dim Logoutstr As String

    Protected Sub Page_PreInit(sender As Object, e As System.EventArgs) Handles Me.PreInit
        Me.Theme = GetGlobalResourceObject("SystemResource", "Theme")
    End Sub

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        rtbUserName.Focus()

        If Not IsPostBack Then

            If Request.QueryString("Logout") IsNot Nothing Then
                Logoutstr = Request.QueryString("Logout").ToString
                If Logoutstr = "1" Then
                    Session("UserInfo") = Nothing
                End If
            End If

            Dim _lblPageTitle As Label
            _lblPageTitle = Me.Master.FindControl("lblPageTitle")
            _lblPageTitle.Text = GetLocalResourceObject("PageTitle")

            rtbUserName.Attributes.Add("onKeyPress", "doClick('" + rbtnLogin.ClientID + "',event)")
            rtbPassword.Attributes.Add("onKeyPress", "doClick('" + rbtnLogin.ClientID + "',event)")

        End If

    End Sub

    Sub CheckUser()
        Dim Users As New UserSP
        Dim UsersDS As New Data.DataSet
        UsersDS = Users.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.FillDataset, "rtvUser", , rtbUserName.Text, , rtbPassword.Text, , )
        If Not UsersDS Is Nothing Then
            If UsersDS.Tables(0).Rows.Count = 0 Then ' or the user does not have any permission 
                'Redirect the page to the unauthorized page
                lblLoginFailed.Visible = True

            Else
                'lblError.Visible = False
                Dim SESCLASS As New SessionClass
                SESCLASS.setUserID(UsersDS.Tables(0).Rows(0).Item("id"))
                SESCLASS.setemp_Num(UsersDS.Tables(0).Rows(0).Item("emp_Num").ToString())
                SESCLASS.setFullName(UsersDS.Tables(0).Rows(0).Item("FullName").ToString())
                SESCLASS.setUserName(UsersDS.Tables(0).Rows(0).Item("UserName").ToString())

                Session("UserInfo") = SESCLASS
                Session("UserName") = UsersDS.Tables(0).Rows(0).Item("UserName").ToString()
                FormsAuthentication.RedirectFromLoginPage(rtbUserName.Text, False)
                Response.Redirect("Default.aspx")
            End If
        End If
    End Sub


    Protected Sub rbtnLogin_Click(sender As Object, e As System.EventArgs) Handles rbtnLogin.Click
        CheckUser()
    End Sub
End Class
